import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    redirect: { name: 'user' },
    children: [
      { path: 'push', name: 'push', component: () => import('../views/attach/PushView.vue') },
      { path: 'times', name: 'times', component: () => import('../views/attach/TimesView.vue') },
      { path: 'code', name: 'code', component: () => import('../views/business/CodeView.vue') },
      { path: 'shop', name: 'shop', component: () => import('../views/business/ShopView.vue') },
      { path: 'xv', name: 'xv', component: () => import('../views/business/XView.vue') },
      { path: 'articleList', name: 'articleList', component: () => import('../views/content/ArticleListView.vue') },
      { path: 'category', name: 'category', component: () => import('../views/content/CategoryView.vue') },
      { path: 'creation', name: 'creation', component: () => import('../views/content/CreationView.vue') },
      { path: 'activity', name: 'activity', component: () => import('../views/coupon/ActivityView.vue') },
      { path: 'addActivity', name: 'addActivity', component: () => import('../views/coupon/CreateActivity.vue') },
      { path: 'coupon', name: 'coupon', component: () => import('../views/coupon/CouponView.vue') },
      { path: 'crowdList', name: 'crowdList', component: () => import('../views/user/CrowdListView.vue') },
      { path: 'user', name: 'user', component: () => import('../views/user/UserView.vue') },
      { path: 'rziew', name: 'rziew', component: () => import('../views/user/RZiew.vue') }
    ],
    meta: { requiresAuth: true } // 需要登录才能访问
  },
  {path: '/login', component: () => import('../views/LoginView.vue')},
  {path: '/register', component: () => import('../views/RegisterView.vue')}


]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 添加路由守卫检查登录状态
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 检查是否已登录
    if (!localStorage.getItem('isAuthenticated')) {
      next('/login');
    } else {
      next();
    }
  } else {
    next();
  }
})

export default router
